﻿using BenchmarkDotNet.Attributes;
using KdbndpTest.Models;
using SqlSugar;

namespace KdbndpTest.BenchmarkRunner
{
    public class Demo2_Updateable
    {
        [Benchmark(Description = "更新单条数据")]
        public void Update()
        {
            var db = GetInstance();

            //Ignore  CreateTime
            var updateObj = new Order() { Id = 1, Name = "order1" };
            var updateObjs = new List<Order> {
                 new Order() { Id = 11, Name = "order11" },
                 new Order() { Id = 12, Name = "order12" }
            };

            var insertId = db.Insertable(updateObj).ExecuteReturnIdentity();

            //update all columns by primary key
            var result = db.Updateable(updateObj).ExecuteCommand();//update single

            db.Deleteable<Order>().In(insertId).ExecuteCommand();
        }

        [Benchmark(Description = "更新多条数据")]
        public void Updateables()
        {
            var db = GetInstance();

            //Ignore  CreateTime
            var updateObjs = new List<Order> {
                 new Order() { Id = 11, Name = "order11" },
                 new Order() { Id = 12, Name = "order12" }
            };

            var insertId = db.Insertable(updateObjs).ExecuteReturnIdentity();

            //update all columns by primary key
            var result2 = db.Updateable(updateObjs).ExecuteCommand();

            db.Deleteable<Order>().In(new int[] { 11, 12 }).ExecuteCommand();

        }

        [Benchmark(Description = "更新满足条件单条数据")]
        public void UpdateWhere()
        {
            var db = GetInstance();

            //Ignore  CreateTime
            var updateObj = new Order() { Id = 1, Name = "order1" };

            var insertId = db.Insertable(updateObj).ExecuteReturnIdentity();

            var result5 = db.Updateable(updateObj).WhereColumns(it => new { it.Id }).ExecuteCommand();//update single by id

            db.Deleteable<Order>().In(insertId).ExecuteCommand();

        }

        [Benchmark(Description = "更新满足条件多条数据")]
        public void UpdateablesWhere()
        {
            var db = GetInstance();

            //Ignore  CreateTime
            var updateObjs = new List<Order> {
                 new Order() { Id = 11, Name = "order11" },
                 new Order() { Id = 12, Name = "order12" }
            };

            var insertId = db.Insertable(updateObjs).ExecuteReturnIdentity();

            var result5 = db.Updateable(updateObjs).WhereColumns(it => new { it.Id }).ExecuteCommand();//update single by id

            db.Deleteable<Order>().In(new int[] { 11, 12 }).ExecuteCommand();

        }


        [Benchmark(Description = "更新满足表达式数据")]
        public void UpdateExpression()
        {
            var db = GetInstance();

            var updateObjs = new List<Order> {
                 new Order() { Id = 11, Name = "order11" },
                 new Order() { Id = 12, Name = "order12" }
            };

            var insertId = db.Insertable(updateObjs).ExecuteReturnIdentity();

            //Ignore  CreateTime
            var result7 = db.Updateable<Order>(it => new Order() { Name = "a", CreateTime = DateTime.Now }).Where(it => it.Id == 11).ExecuteCommand();
            var result71 = db.Updateable<Order>().SetColumns(it => new Order() { Name = "a", CreateTime = DateTime.Now }).Where(it => it.Id == 12).ExecuteCommand();

            db.Deleteable<Order>().In(new int[] { 11, 12 }).ExecuteCommand();
        }

        [Benchmark(Description = "If Else条件更新数据")]
        public void UpdateIf()
        {
            var db = GetInstance();

            var updateObj = new Order() { Id = 1, Name = "order1" };
            var caseValue = "1";

            var insertId = db.Insertable(updateObj).ExecuteReturnIdentity();

            //Ignore  CreateTime
            db.Updateable(updateObj)
                            .UpdateColumnsIF(caseValue == "1", it => new { it.Name })
                            .UpdateColumnsIF(caseValue == "2", it => new { it.Name, it.CreateTime })
                            .ExecuteCommand();

            db.Deleteable<Order>().In(1).ExecuteCommand();

        }


        [Benchmark(Description = "并发锁更新数据")]
        public void UpdateUpdLock()
        {
            var db = GetInstance();

            var updateObj = new Order() { Id = 1, Name = "order1" };

            var insertId = db.Insertable(updateObj).ExecuteReturnIdentity();

            //Ignore  CreateTime
            db.Updateable(updateObj).With(SqlWith.UpdLock).ExecuteCommand();

            db.Deleteable<Order>().In(1).ExecuteCommand();
        }

        private static SqlSugarClient GetInstance()
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.Kdbndp,
                ConnectionString = Config.ConnectionString,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                MoreSettings = new ConnMoreSettings()
                {
                    DatabaseModel = DbType.PostgreSQL
                },
                AopEvents = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            //If no exist create datebase 
            db.DbMaintenance.CreateDatabase();

            //Use db query
            var dt = db.Ado.GetDataTable("select 1");

            //Create tables
            db.CodeFirst.InitTables(typeof(OrderItem), typeof(Order));

            return db;
        }
    }
}
